package org.ala.shopping_test.shopping_center.repository.order_buyer.router;

import java.util.List;
import java.util.Map;

import org.ala.shopping_test.shopping_center.repository.order_buyer.entity.ShoppingOrderBuyerDO;
import org.ala.shopping_test.shopping_center.repository.shard_key.ShoppingOrderBuyerSellerShardKey;
import org.ala.shopping_test.shopping_center.repository.shard_key.ShoppingOrderBuyerSellerShardKeyRouter;
import org.ala.tiktools.sharddb.mybatis.router.IShardRouter;
import org.ala.tiktools.sharddb.mybatis.router.ShardInfo;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKeyConverter;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKeyRouter;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.ShardKeyRouteWorker;

/**
 *	分库分表路由器 (shopping_order_buyer)
 *  订单总表
 *
 *	@author 骆毅 (ala)
 *	@date 2025-03-21 12:09:46
 */
public class ShoppingOrderBuyerDOListRouter implements IShardRouter<List<ShoppingOrderBuyerDO>> {
    /**
     *  分片建路由器
     */
    protected IShardKeyRouter<ShoppingOrderBuyerSellerShardKey> router = new ShoppingOrderBuyerSellerShardKeyRouter();
    /**
     *  分片建转换器
     */
    protected IShardKeyConverter<ShoppingOrderBuyerDO, ShoppingOrderBuyerSellerShardKey> converter = new IShardKeyConverter<ShoppingOrderBuyerDO, ShoppingOrderBuyerSellerShardKey>() {
        @Override
        public ShoppingOrderBuyerSellerShardKey convert(ShoppingOrderBuyerDO d) {
            return ShoppingOrderBuyerSellerShardKey.build()
                    .id(d.getBuyerId())
                    .timestamp(d.getCreateTime());
        }

        @Override
        public ShoppingOrderBuyerDO deConvert(ShoppingOrderBuyerDO d, ShoppingOrderBuyerSellerShardKey k) {
            return d.copy()
                    .buyerId(k.getId())
                    .createTime(k.getTimestamp());
        }
    };


    /**
     * 分库分表
     */
    @Override
    public Map<ShardInfo, List<ShoppingOrderBuyerDO>> route(List<ShoppingOrderBuyerDO> d) {
        return ShardKeyRouteWorker.instance().route(d, router, converter);
    }
}